<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class SmartStore extends ShopFrontEndController {

	public function __construct($params) {
		parent::__construct($params);
	}

	public function renderView($viewName, $data = null) {
		$theme_path          = 'shop-themes/'. $this->shop->template;
		$data['shop']        = $this->shop;
		$data['shop_page']   = $this->shop_model->getShopPages($this->shop->show_page);
		$data['shop_cate']   = $this->shop_model->getAllShopCategory($this->shop->getSlug());
		$data['html']        = $this->load->view($theme_path . '/pages/' . $viewName . '.html.php', $data, true);
		$this->load->view($theme_path . '/layout/layout.html.php', $data);
	}

	public function index() {
		$aProductHighlight = $this->shop_product_model->getHighlightProducts($this->shop->getId(), SMARTSTORE_HOME_PRODUCT_LIMIT);
		$shop_slides = $this->shop_model->getBannerSlide($this->shop->getSlug());
		$aReturn = array(
			'shop_slides'       => $shop_slides,
			'aProductHighlight' => $aProductHighlight
			);
		$this->renderView('index', $aReturn);
	}

	public function contact($shop_slug) {
		$this->load->model('repo/user_model', 'user_model');
		$user_info = $this->user_model->getUserByShopSlug($this->shop->getSlug());
		$this->frontend_lib->setAJs("https://www.google.com/recaptcha/api.js?hl=vi");
		$aReturn = array('pageTitle'=>'Liên hệ', 'user_info'=>$user_info);
		$this->renderView('contact', $aReturn);
	}

	private function intialPagingSearch($searchkey) {
		$this->mypaging->setLimit($this->shop->shop_product_num);
		$this->mypaging->setSuffix($this->config->item('url_suffix'));
		$this->mypaging->setCount($this->shop_product_model->countFrontSearchProduct($this->shop->getId(), $searchkey));
		$this->mypaging->setMainURL(shop_product_search_paging_url($this->shop->getSlug()));
	}

	public function search($params=array() ) {
		$pageIndex= count($params)>0 && isset($params[1]) ? $params[1] : 1;
		$this->load->library('mypaging');
		$searchkey = $this->input->get('s', true);
		$this->intialPagingSearch($searchkey);
		$pagingBar = $this->mypaging->getBar($pageIndex, array('s' => $searchkey));
		$offset    = ($pageIndex - 1) * $this->shop->shop_product_num;
		$aProduct  = $this->shop_product_model->searchFrontProduct($this->shop->getId(), $searchkey, $this->shop->shop_product_num, $offset);
		$aReturn   = array(
			'pageTitle'      => 'Sản phẩm',
			'aProduct'       => $aProduct,
			'pagingBar'      => $pagingBar,
			'searchkey'      => $searchkey
			);
		$this->renderView('search', $aReturn);
	}
}

?>